﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class GetSize
{
    /// <summary>
    /// 获取对象的转换为二进制的字节大小
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static long GetByteSize(object obj)
    {
        long result;
        BinaryFormatter bFormatter = new BinaryFormatter();
        using (MemoryStream stream = new MemoryStream())
        {
            bFormatter.Serialize(stream, obj);
            result = stream.Length;
        }
        return result;
    }
}